﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;
using System.Collections.Generic;
namespace Login
{
    public class LoginUserLogin : Fsm.FsmStateMonoBase<LoginState>
    {
        LoginManager manager = null;
        public LoginUserLogin(LoginState state, LoginManager ctrl) : base(state, ctrl) { manager = ctrl; }

        public override void Enter()
        {
            Debug.Log("[DEBUG] LoginQuickLogin.Enter()");
            //base.Enter();
            UserLogin();
        }

        void UserLogin()
        {
            
            CSUserLogin msg = new CSUserLogin();
            msg.channelId = GameConfig.Instance.GetChannelId;
            msg.channelName = GameConfig.Instance.GetChannelName;
            msg.platformUid = StaticData.UserName;
            msg.authCode = StaticData.PassWord;
            if (GameConfig.Instance.platform == 1)
            {
                msg.loginType = 2;
               
                msg.deviceId ="";//唯一的设备标示
                msg.deviceDesc = "";//设备型号
              
            }
            else if (GameConfig.Instance.platform == 2)
            {
                msg.loginType = 1;
                //msg.platformUid = LoginPlatform._instance.uid;
                //msg.authCode = LoginPlatform._instance.data;
              
                Debug.Log("uid = " + msg.platformUid + " code = " + msg.authCode);
                msg.deviceId = GameConfig.Instance.GetDeviceID;//唯一的设备标示
				msg.deviceDesc = SystemInfo.deviceModel;//设备型号
                
            }

            msg.activeCode = "testActiveCode";//暂时没用
            msg.appVersion = GameConfig.Instance.AppVersion;
            msg.resVersoin = GameConfig.Instance.ResVersion;
           

            msg.clientTime = ServerTimerTool.ClientUtcJavaTime();//Battle.Engine.CommonUtil.GetTimeNow();

            NetAPI.SendHttp(OpDefine.CSUserLogin, msg, UserLoginCallBack);

        }

        void UserLoginCallBack(NetWork.Packets.Packet packet, bool bSuccess)
        {
            if (bSuccess)
            {

                SCUserLogin msg = packet.kBody as SCUserLogin;
                if (msg != null)
                {
                    //计算客户端与服务器时间差值
                    long clientTimeNow = Battle.Engine.CommonUtil.GetTimeNow();
                    StaticData.clientServerTimeDelta = msg.serverTimeNow - (msg.clientTime + clientTimeNow) / 2;
                    ServerTimerTool.CorrectTime(msg.serverTimeNow);
                    StaticData.playerData.playerId = msg.playerId;

                    manager.isFirstCreate = msg.isFirstCreated;
                    StaticData.userIp = msg.clientIp;
                    if (!msg.isFirstCreated)
                    {
                        manager.SwitchState(LoginState.PlayerLogin);
                    }
                    else
                    {
                        manager.SwitchState(LoginState.GenerateName);
                    }
                    StaticData.socketIp = msg.ip;
                    StaticData.socketPort = msg.port;
                    StaticData.sessionKey = msg.sessionKey;

                    #region Bugly 设置UserID
                    if (string.IsNullOrEmpty(StaticData.playerData.playerId.ToString()))
                    {//还没有playerID的时候,用SuperUID
                        BuglyAgent.SetUserId("Server:" + StaticData.playerData.serverUID + ";SuperUID:" + StaticData.UserName);
                    }
                    else
                    {
                        BuglyAgent.SetUserId("Server:" + StaticData.playerData.serverUID + ";PlayerID:" + StaticData.playerData.playerId);
                    }
                    #endregion
                }
                else
                {
                    StaticData.isLogining = false;
                }


            }
            else
            {
                StaticData.isLogining = false;
            }

        }

    }
}
